package com.hoopchina.settings;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import com.hoopchina.news.R;
import com.hoopchina.util.update.Update;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;

public class CheckUpdate extends Preference {

    private static final String  TAG                   = "CheckUpdate";
    private static final boolean ALLOW_DOWNLOAD        = true;         // temp
    private static final int     NEW_VERSION_EXIST     = 0;
    private static final int     NEW_VERSION_EXIST_NOT = 1;
    private static final int     OPT_POSITIVE          = 0;
    private static final int     OPT_NEGATIVE          = 1;
    private static final int     THREESECOND           = 3000;
    private static final int     ONESECOND             = 1000;
    private Context              mContext;
    private ProgressDialog       progressDialog;

    public CheckUpdate( Context context, AttributeSet attrs ) {
        super( context, attrs );
        init( context );
    }

    public CheckUpdate( Context context, AttributeSet attrs, int defStyle ) {
        super( context, attrs, defStyle );
        init( context );
    }

    @Override
    protected void onClick() {
        super.onClick();
        startChecking( mContext );

    }

    private void startChecking( final Context aContext ) {
        Log.i( TAG, "startChecking" );
        if ( aContext == null )
            return;

        File file = new File( "/sdcard/baidunews/apk", "newsandroid.apk" );
        if ( file.exists() ) {
            file.delete();
        }
        if ( ALLOW_DOWNLOAD ) {
            progressDialog = ProgressDialog.show( aContext,
                    aContext.getString( R.string.app_checkupdate_detecting ),
                    aContext.getString( R.string.app_checkupdate_wait ), true, true );

            final Update up = new Update( aContext );
            Timer timer = new Timer();
            final Handler handler = new Handler() {

                public void handleMessage( Message msg ) {
                    progressDialog.dismiss();
                    AlertDialog altDlg;
                    AlertDialog altDlg1;
                    switch ( msg.what ) {
                        case NEW_VERSION_EXIST:
                        String[] choices = {
                                aContext.getString( R.string.app_checkupdate_opt_positive ),
                                aContext.getString( R.string.app_checkupdate_opt_negative ) };
                        altDlg = new AlertDialog.Builder( aContext )
                                .setTitle( aContext.getString( R.string.app_checkupdate_available ) )
                                .setItems( choices, new DialogInterface.OnClickListener() {

                                    public void onClick( DialogInterface dialoginterface, int res ) {

                                        switch ( res ) {
                                            case OPT_POSITIVE:
                                            if ( ALLOW_DOWNLOAD ) {
                                                up.downloadNewApk( aContext );
                                            }
                                                break;
                                            case OPT_NEGATIVE:
                                            break;
                                    }
                                }
                                } ).setOnCancelListener( new DialogInterface.OnCancelListener() {
                                    public void onCancel( DialogInterface dialog ) {}
                                } ).create();
                        altDlg.show();
                            break;

                        case NEW_VERSION_EXIST_NOT:
                        altDlg1 = new AlertDialog.Builder( aContext )
                                .setTitle( aContext.getString( R.string.app_checkupdate_unvailable ) )
                                .setPositiveButton(
                                        aContext.getString( R.string.app_checkupdate_confirm ),
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick( DialogInterface dialog, int which ) {}
                                        } )
                                .setOnCancelListener( new DialogInterface.OnCancelListener() {
                                    public void onCancel( DialogInterface dialog ) {}
                                } ).create();
                        altDlg1.show();
                            break;
                    }

                    super.handleMessage( msg );
                }
            };
            TimerTask task = new TimerTask() {

                public void run() {
                    if ( progressDialog.isShowing() ) {
                        Message msg = new Message();
                        if ( up.needUpdate() ) {
                            msg.what = NEW_VERSION_EXIST;
                        }
                        else {
                            msg.what = NEW_VERSION_EXIST_NOT;
                        }
                        handler.sendMessage( msg );
                    }
                    else {}
                }
            };
            timer.schedule( task, THREESECOND );
        }
        else {
            Toast.makeText( aContext, aContext.getString( R.string.app_no_network ), ONESECOND )
                    .show();
        }
    }

    private void init( Context aContext ) {
        this.mContext = aContext;
    }
}